package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.game.utils.NotifyModules;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Random;

import static com.kitty.game.utils.Const.MONEY_NAME;
import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

/**
 * 普通鉴定 //DELICATE
 */
@Component("EquipIdentify")
public class EquipIdentifyHandler extends UpgradeHandler {

    private static final String NO_NOT_IDENTITY = "很抱歉，#R{0}#n不是未鉴定装备。";
    private static final String REMOVE_FAIL = "发生错误，请重试。";
    static final String SUCCESS = "恭喜你！花费了{0}文钱，成功的鉴定出了#R{1}#n。";
    private static final String IDENTIFY_MESSAGE = "由于你鉴定的#R{0}#n的属性值较低，#R天鉴神兵#n完成度增加至#R{1}%#n。当完成度达到#R100%#n时，下次鉴定的装备必定有一条属性值为满！";
    private static final String FULLFIELD_MESSAGE = "听闻#Y%s#n鉴定出了满属性装备#R%s#n，真是好运气啊！#3m";

    void pushIdntifyMessage(Role role,RoleEquip roleEquip) {
        String msg = "%.2f";
        MessagePusher.pushMessage(role, new RespNotifyMiscEx(MessageFormat.format(IDENTIFY_MESSAGE,roleEquip.getName(), String.format(msg, role.getEquipIdentify() / 100.0))));
    }

    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip roleEquip = (RoleEquip) params[0];
        short newPos = (short) params[1];
        /**扣除金钱*/
        int needCash = needCash(roleEquip.queryLevel(), roleEquip.queryEquipPos(false));
        roleService.subtractMoney(role, needCash);

        /**增加鉴定度*/
        addIdentify(role);

        short identify = role.getEquipIdentify();
        Random random = new Random();
        int successRate = random.nextInt(10000) + 1;

        RoleEquip newEquip = null;
        if (identify >= 10000 || successRate < identify) {
            role.setEquipIdentify((short) 0);
            newEquip = equipService.identifyEquip(role, roleEquip.getName(), newPos, true);
            pushFullFieldEquip(role,newEquip);
        } else {
            newEquip = equipService.identifyEquip(role, roleEquip.getName(), newPos, false);
            pushIdntifyMessage(role,newEquip);
        }
        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_EQUIP_IDENTIFY);//装备鉴定成功
        respGeneralNotify.setValue(newEquip.getId() + "|1");
        MessagePusher.pushMessage(role, respGeneralNotify);
        pushOperateResult(role,UPGRADE_SUCCESS);
        return MessageFormat.format(SUCCESS, AsktaoUtil.getMoneyFormat(needCash), newEquip.getName());
    }

    private void pushFullFieldEquip(Role role, RoleEquip roleEquip){
        String msg =String.format(FULLFIELD_MESSAGE,role.getName(),roleEquip.getName());
        SpringUtils.getBroadcastService().sendRumor(msg,null);
    }
    void addIdentify(Role role) {
        roleService.addAchieveScore(role,"301019");
        role.setEquipIdentify((short) (role.getEquipIdentify() + 1));
        roleService.updateIdentify(role);
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {

        short newPos =getVacancy(role);
        if (newPos <= 0) {
            return ROLE_VACANCY_NOT_ENOUGH;
        }
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if(roleEquip==null){
            return "请放入正确的装备。";
        }
        if (roleEquip.queryLevel()<10){
            return "装备等级过低，无法鉴定。";
        }
        if (!isRightPosition(roleEquip.queryEquipPos(false))) {
            return MessageFormat.format(NO_RIGHT_POSITION, "鉴定", roleEquip.getName());
        }
        if (!roleEquip.getType().equals("未鉴定装备")) {
            return MessageFormat.format(NO_NOT_IDENTITY, roleEquip.getName());
        }
        String noEnough = checkResource(role, roleEquip.queryUpgradeLevel(), roleEquip.queryEquipPos(false));
        if (noEnough != null) {
            return noEnough;
        }

        if (equipService.delEquipNum(role, roleEquip, 1) <= 0) {
            return REMOVE_FAIL;
        }
        return doUpgrade(role, roleEquip, newPos);
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        short level = (short) objects[1];
        byte equipPos = (byte) objects[2];

        if (role.getMoney() < needCash(level, equipPos)) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
        }
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        int needCash = 0;
        if (pos == 1) {
            if (level == 130) {
                needCash = 600000;
            } else if (level == 120) {
                needCash = 600000;
            } else if (level == 110) {
                needCash = 500000;
            } else if (level == 100) {
                needCash = 400000;
            } else if (level == 90) {
                needCash = 300000;
            } else if (level == 80) {
                needCash = 200000;
            } else if (level == 70) {
                needCash = 150000;
            } else if (level == 60) {
                needCash = 100000;
            } else if (level == 50) {
                needCash = 50000;
            }
        } else if (pos == 2 || pos == 10) {
            if (level == 130) {
                needCash = 600000;
            } else if (level == 120) {
                needCash = 500000;
            } else if (level == 110) {
                needCash = 400000;
            } else if (level == 100) {
                needCash = 300000;
            } else if (level == 90) {
                needCash = 200000;
            } else if (level == 80) {
                needCash = 100000;
            } else if (level == 70) {
                needCash = 75000;
            } else if (level == 60) {
                needCash = 50000;
            } else if (level == 50) {
                needCash = 25000;
            }
        } else if (pos == 3) {
            if (level == 130) {
                needCash = 600000;
            } else if (level == 120) {
                needCash = 750000;
            } else if (level == 110) {
                needCash = 600000;
            } else if (level == 100) {
                needCash = 450000;
            } else if (level == 90) {
                needCash = 300000;
            } else if (level == 80) {
                needCash = 200000;
            } else if (level == 70) {
                needCash = 150000;
            } else if (level == 60) {
                needCash = 100000;
            } else if (level == 50) {
                needCash = 50000;
            }
        }
        return Math.max(needCash, 0);
    }


    @Override
    public String getUpdateTypeStr() {
        return "equip_identity";
    }

}
